﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DcsGauges.Shared.Network;
using DcsGauges.Testing.Utils;
using DcsGauges.Shared.Exceptions;

namespace DcsGauges.Shared.Tests
{
    [TestClass]
    public class StringBufferTests
    {
        [TestMethod]
        public void TestCannotCreateZeroSizedBuffer()
        {
            StringBuffer buffer = null;
            ThrowAssert.Throws<DcsArgumentException>(() => { buffer = new StringBuffer(0); });
        }

        [TestMethod]
        public void TestSimpleAppend()
        {
            string toAppend         = "test";
            var buffer              = CreateFilledBuffer(toAppend);
            string inBuffer         = buffer.ContentString;
            Assert.AreEqual(toAppend, inBuffer);
        }

        [TestMethod]
        public void TestMultipleAppends()
        {
            List<string> toAppend           = new List<string>();
            List<byte[]> toAppendData       = new List<byte[]>();

            StringBuffer buffer             = new StringBuffer();

            for (int i = 0; i < 10; i++)
            {
                string temp                 = Guid.NewGuid().ToString("N");
                byte[] tempData             = StringBuffer.UsedEncoding.GetBytes(temp);

                toAppend.Add(temp);
                toAppendData.Add(tempData);
                buffer.Append(tempData, tempData.Length);
            }

            string final = string.Join("", toAppend);
            Assert.AreEqual(final, buffer.ContentString);
        }

        [TestMethod]
        public void TestSubArrayAppend()
        {
            string firstPart                = Guid.NewGuid().ToString("N");
            string secondPart               = Guid.NewGuid().ToString("N");
            string concat                   = firstPart + secondPart;

            var firstPartData               = new List<byte>(StringBuffer.UsedEncoding.GetBytes(firstPart));
            var secondPartData              = new List<byte>(StringBuffer.UsedEncoding.GetBytes(secondPart));
            var allData                     = firstPartData.Concat(secondPartData).ToArray();

            StringBuffer buffer             = new StringBuffer();
            buffer.Append(allData, firstPartData.Count);

            Assert.AreEqual(firstPart, buffer.ContentString);
        }

        [TestMethod]
        public void TestInterchangingAppendAndClear()
        {
            string content          = Guid.NewGuid().ToString("N");
            var buffer              = CreateFilledBuffer(content);
            Assert.AreEqual(content, buffer.ContentString);
            buffer.Clear();

            Assert.AreEqual(string.Empty, buffer.ContentString);
        }

        [TestMethod]
        public void TestMultipleClearCalls()
        {
            StringBuffer buffer = new StringBuffer();
            buffer.Clear();
            buffer.Clear();
            buffer.Clear();
        }


        private StringBuffer CreateFilledBuffer(string content)
        {
            byte[] data             = StringBuffer.UsedEncoding.GetBytes(content);
            StringBuffer buffer     = new StringBuffer();
            buffer.Append(data, data.Length);
            return buffer;
        }

    }
}
